// pages/home/details.js
const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    box_id: 0,
    box_details: {},
    box_price: 0,
    buy_time: [],
    buytimeSelect: 0,
    default_brand: '',       //详情页显示的已选品牌
    default_amount: '常量',        //详情页显示的已选月经量
    default_amountSelect: 1,       //详情页显示的已选月经量的id
    default_boxContent: [],    //详情页显示的盒子内容
    amount_tpye: [{ value: 0, name: '少量' }, { value: 1, name: '常量' }, { value: 2, name: '大量' }],
    amountSelect: 1,               //弹出框选择的月经量的id
    amountName: '常量',            //弹出框选择的月经量

    // 评论
    comment_list: [
      {
        id: 1,
        user_img: '/image/circle_head1.jpg',
        user_name: 'chinchin',
        content: '超级无敌漂亮超级无敌漂亮超级无敌漂亮超级无敌漂亮！！',
        com_img: [
          '/image/circle_head1.jpg',
          '/image/circle2.jpg',
          '/image/rb.jpg'
        ],
        com_img_len: 3,
        time: '2019-2-6'
      },
      {
        id: 1,
        user_img: '/image/circle_head3.jpg',
        user_name: '厦门厦门',
        content: '很漂亮！！！',
        com_img: [
          '/image/circle_head3.jpg'
        ],
        com_img_len: 1,
        time: '2019-1-6'
      },
      {
        id: 1,
        user_img: '/image/circle_head4.jpg',
        user_name: '海沧',
        content: '美美哒！',
        com_img: [],
        com_img_len: 0,
        time: '2018-12-6',
      }
    ],
    comment_num: 3,

    // 弹出框
    showModalStatus: false,       //是否显示弹出框
    brand_id: 0,                  //弹出框选择的品牌ID
    brand_txt: '',                //弹出框选择的品牌名字
    brand_boxContent: [],         //弹出框选择的盒子内容
    // gg_price: 0,//规格价格
    // guigeList: [{ guige: '100', price: '150' }, { guige: '200', price: '150' }, { guige: '300', price: '150' }],
    brandList: [],
    num: 1,//初始数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.id);
    if (options.id) {
      this.setData({
        box_id: options.id
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getBoxdetails(this.data.box_id);
    this.getBoxprice(this.data.box_id);
    this.getBoxbrand(this.data.box_id);
    this.getBoxEvaluate();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  buytimeSelect: function (e) {
    console.log(e);
    var curPrice = 0;
    for (var i = 0; i < this.data.buy_time.length; i++) {
      if (this.data.buy_time[i].month == e.currentTarget.dataset.month) {
        curPrice = this.data.buy_time[i].price;
      }
    }
    this.setData({
      buytimeSelect: e.currentTarget.dataset.month,
      box_price: curPrice
    });
  },
  amountSelect: function (e) {
    // console.log(e);
    this.setData({
      amountSelect: e.currentTarget.dataset.value
    });
  },
  // 获取盒子详情 
  getBoxdetails: function (boxId) {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getBoxDetails',
      method: 'GET',
      data: {
        boxId: boxId
      },
      success: function (e) {
        console.log(e);
        that.setData({
          box_details: e.data
        });
      }
    })
  },
  // 获取盒子价格
  getBoxprice: function (boxId) {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getBoxPrice',
      method: 'GET',
      data: {
        boxId: boxId
      },
      success: function (e) {
        console.log(e);
        that.setData({
          buy_time: e.data,
          buytimeSelect: e.data[0].month,
          box_price: e.data[0].price
        });
      }
    })
  },
  // 是否注册
  isRegister: function () {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'isRegister',
      method: 'GET',
      data: {
        openid: app.globalData.openid
      },
      success: function (e) {
        // console.log(e);
        if (e.data.status == 1) {
          that.placeOrder();
        } else {
          wx.showToast({
            title: '请先登录',
            icon: 'loading',
            duration: 800,
            success: function (res) {
              setTimeout(function () {
                wx.switchTab({
                  url: '/pages/me/index'
                })
              }, 800);
            }
          })
        }
      }
    })
  },
  // 下单
  placeOrder: function () {
    // console.log(e);
    // console.log(this.data.box_id,this.data.box_price,this.data.default_brand);
    // console.log(this.data.buy_time);
    // console.log(this.data.buytimeSelect);
    // console.log(this.data.default_amount);

    var that = this;
    // 判断一下是否有生成订单
    wx.request({
      url: app.globalData.serverUrl + 'hasForeverOrder',
      method: 'GET',
      data: {
        openid: app.globalData.openid
      },
      success: function (e) {
        if (e.data.status == 1) {
          that.generateOrder();
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: '您已经下过单,到我的永久订单支付或者续费即可！',
            confirmColor: '#fc5871'
          })
        }
      }
    })
  },
  // 生成订单
  generateOrder: function () {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'generateOrder',
      method: 'GET',
      data: {
        openid: app.globalData.openid,
        box_id: that.data.box_id,
        sum_box: that.data.buytimeSelect,
        sum_price: that.data.box_price,
        menstrual_volume: that.data.default_amount,
        brand: that.data.default_brand,
        pay_type:0
      },
      success: function (e) {
        // console.log(e);
        if(e.data){
          wx.navigateTo({
            // 跳转传参算数
            url: '/pages/home/order?id=' + e.data.id
          })
        }
      }
    })
  },

  // 盒子信息图片预览
  swiperYu: function (e) {
    console.log(e);
    wx.previewImage({
      current: e.currentTarget.dataset.curimg, // 当前显示图片的http链接
      urls: e.currentTarget.dataset.arrimg // 需要预览的图片http链接列表
    })
  },

  // 获取盒子评论
  getBoxEvaluate:function(){
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getBoxEvaluate',
      method:'GET',
      data:{
        box_id:this.data.box_id
      },
      success:function(e){
        console.log(e);
        that.setData({
          comment_list: e.data.evaluate_list,
          comment_num: e.data.evaluate_num
        });
      }
    })
  },

  // 评论图片预览
  imgYu: function (e) {
    console.log(e);
    wx.previewImage({
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
      urls: e.currentTarget.dataset.arrurl// 需要预览的图片http链接列表
    })
  },
  // 评论查看更多
  seeMore: function () {
    var boxId = this.data.box_id;
    wx.navigateTo({
      // 跳转传参算数
      url: '/pages/home/boxEvaluate?id=' + boxId
    })
  },

  // 获取盒子的默认品牌信息
  getBoxbrand: function (boxId) {
    var that = this;
    // 获取盒子默认的品牌信息
    wx.request({
      url: app.globalData.serverUrl + 'getBoxbrand',
      method: 'GET',
      data: {
        boxId: boxId
      },
      success: function (e) {
        // console.log(e);
        that.setData({
          default_brand: e.data.brand,
          default_boxContent: e.data.content,
          brand_id: e.data.id,
          brand_txt: e.data.brand,
          brand_boxContent: e.data.content
        });
        // 获取该盒子所有品牌信息
        that.getBoxAllbrand(boxId);
      }
    })
  },
  // 获取该盒子所有品牌信息
  getBoxAllbrand: function (boxId) {
    var that = this;
    // 获取该盒子所有品牌信息
    wx.request({
      url: app.globalData.serverUrl + 'getBoxAllbrand',
      method: 'GET',
      data: {
        boxId: boxId
      },
      success: function (e) {
        // console.log(e);
        that.setData({
          brandList: e.data
        });
      }
    })
  },


  // 弹出框
  // 选择品牌
  filter: function (e) {
    console.log(e);
    // var self = this, id = e.currentTarget.dataset.id, txt = e.currentTarget.dataset.txt, price = e.currentTarget.dataset.price
    // self.setData({
    //   gg_id: id,
    //   gg_txt: txt,
    //   gg_price: price
    // });
    this.setData({
      brand_id: e.currentTarget.dataset.id,
      brand_txt: e.currentTarget.dataset.txt,
      brand_boxContent: e.currentTarget.dataset.content
    });
  },
  // 选择月经量
  filterType: function (e) {
    console.log(e);
    this.setData({
      amountSelect: e.currentTarget.dataset.value,
      amountName: e.currentTarget.dataset.name
    });
  },
  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减  
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    // 不作过多考虑自增1  
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },

  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  // 选择好品牌和月经量
  selectdOk: function () {
    this.setData({
      default_amount: this.data.amountName,
      default_amountSelect: this.data.amountSelect,
      default_brand: this.data.brand_txt,
      default_boxContent: this.data.brand_boxContent
    });
    this.hideModal();
  }
})